﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using KC.Framework.Extension;
using KC.Framework.Tenant;
using KC.Framework.Util;
using Microsoft.Extensions.Configuration;

namespace KC.Framework.Base
{
    public class GlobalConfig
    {
        #region AppSetting configuration

        public static string ApplicationId { get; set; }
        public static Guid ApplicationGuid { get; set; }
        public static string ApplicationName { get; set; }
        public static string ApplicationWebSiteName { get; set; }
        public static string EncryptKey { get; set; }
        public static string BlobStorage { get; set; }
        public static string AdminEmails { get; set; }
        public static string TempFilePath { get; set; }

        public static string DatabaseConnectionString { get; set; }
        public static string MySqlConnectionString { get; set; }
        public static string StorageConnectionString { get; set; }
        public static string QueueConnectionString { get; set; }
        public static string NoSqlConnectionString { get; set; }
        public static string RedisConnectionString { get; set; }
        public static string ServiceBusConnectionString { get; set; }

        public static string ClientId { get; set; }
        public static string ClientSecret { get; set; }

        public static string WeixinAppKey { get; set; }
        public static string WeixinAppSecret { get; set; }
        public static string WeixinAppToken { get; set; }

        public static string ZZZLicenseName { get; set; }
        public static string ZZZLicenseKey { get; set; }

        #endregion

        #region Domain
        public static List<ApplicationInfo> Applications { get; set; }
        public static ApplicationInfo CurrentApplication { get; set; }

        /// <summary>
        /// subdomain的sso地址：http://sso.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1001/
        /// </summary>
        public static string SSOWebDomain { get; set; }
        /// <summary>
        /// subdomain的admin地址：http://admin.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1003
        /// </summary>
        public static string AdminWebDomain { get; set; }
        /// <summary>
        /// subdomain的Blog地址：http://blog.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1005
        /// </summary>
        public static string BlogWebDomain { get; set; }

        /// <summary>
        /// subdomain的config地址：http://subdomain.cfg.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1101/
        /// </summary>
        public static string CfgWebDomain { get; set; }
        /// <summary>
        /// subdomain的dictionary地址：http://subdomain.dic.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1103/
        /// </summary>
        public static string DicWebDomain { get; set; }
        /// <summary>
        /// subdomain的app地址：http://subdomain.app.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1105
        /// </summary>
        public static string AppWebDomain { get; set; }
        /// <summary>
        /// subdomain的message地址：http://subdomain.msg.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1107/
        /// </summary>
        public static string MsgWebDomain { get; set; }

        /// <summary>
        /// subdomain的account地址：http://subdomain.acc.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2001/
        /// </summary>
        public static string AccWebDomain { get; set; }
        /// <summary>
        /// subdomain的contract地址：http://subdomain.econ.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2003/
        /// </summary>
        public static string EconWebDomain { get; set; }
        /// <summary>
        /// subdomain的document地址：http://subdomain.doc.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2005/
        /// </summary>
        public static string DocWebDomain { get; set; }
        /// <summary>
        /// subdomain的Hr地址：http://subdomain.hr.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2007/
        /// </summary>
        public static string HrWebDomain { get; set; }

        /// <summary>
        /// subdomain的crm地址：http://subdomain.crm.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3001/
        /// </summary>
        public static string CrmWebDomain { get; set; }

        /// <summary>
        /// subdomain的srm地址：http://subdomain.srm.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3003/
        /// </summary>
        public static string SrmWebDomain { get; set; }

        /// <summary>
        /// subdomain的prd地址：http://subdomain.prd.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3005/
        /// </summary>
        public static string PrdWebDomain { get; set; }

        /// <summary>
        /// subdomain的pmc地址：http://subdomain.pmc.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3007/
        /// </summary>
        public static string PmcWebDomain { get; set; }

        /// <summary>
        /// subdomain的电商地址：http://subdomain.portal.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4001/
        /// </summary>
        public static string PortalWebDomain { get; set; }
        /// <summary>
        /// subdomain的som地址：http://subdomain.som.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4003/
        /// </summary>
        public static string SomWebDomain { get; set; }

        /// <summary>
        /// subdomain的pom地址：http://subdomain.pom.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4005/
        /// </summary>
        public static string PomWebDomain { get; set; }

        /// <summary>
        /// subdomain的wms地址：http://subdomain.wms.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4007/
        /// </summary>
        public static string WmsWebDomain { get; set; }

        /// <summary>
        /// subdomain的融资地址：http://subdomain.market.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:5001/
        /// </summary>
        public static string JRWebDomain { get; set; }

        /// <summary>
        /// subdomain的培训管理地址：http://subdomain.trn.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:6001/
        /// </summary>
        public static string TrainWebDomain { get; set; }

        /// <summary>
        /// subdomain的工作流地址：http://subdomain.flow.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:7001/
        /// </summary>
        public static string FlowWebDomain { get; set; }
        /// <summary>
        /// subdomain的sso地址：http://subdomain.pay.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:8001/
        /// </summary>
        public static string PayWebDomain { get; set; }
        /// <summary>
        /// subdomain的微信地址：http://subdomain.wx.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:9001/
        /// </summary>
        public static string WXWebDomain { get; set; }
        /// <summary>
        /// subdomain的资源地址：http://resource.kcloudy.com/
        ///     本地测试接口地址：http://localhost:9009/
        /// </summary>
        public static string ResWebDomain { get; set; }

        /// <summary>
        /// subdomain的接口地址，无api/后缀：http://subdomain.api.kcloudy.com/
        /// </summary>
        public static string ApiWebDomain { get; set; }

        /// <summary>
        /// 获取webDomain的租户域名地址：http://[tenantName].[webDomain].kcloudy.com/
        ///     本地测试地址：http://[tenantName].localhost:1002/
        /// </summary>
        /// <param name="tenantName"></param>
        /// <returns></returns>
        public static string GetTenantWebDomain(string webDomain, string tenantName)
        {
            if (string.IsNullOrEmpty(webDomain))
                return null;
            return webDomain.Replace(TenantConstant.SubDomain, tenantName);
        }
        #endregion

        #region Api Url

        /// <summary>
        /// subdomain的接口地址：http://subdomain.api.kcloudy.com/api/
        /// </summary>
        public static string ApiServerUrl
        {
            get
            {
                if (!string.IsNullOrEmpty(ApiWebDomain))
                {
                    return ApiWebDomain + "api/";
                }
                return null;
            }
        }

        /// <summary>
        /// 获取租户信息接口地址：http://adminapi.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1004/api/
        /// </summary>
        public static string AdminApiUrl
        {
            get
            {
                if (string.IsNullOrEmpty(AdminWebDomain))
                    return null;

                return AdminWebDomain.Replace("admin.", "adminapi.")
                        .Replace(":1003", ":1004").Replace(":1013", ":1014") + "api/";
            }
        }

        /// <summary>
        /// 获取租户信息接口地址：http://blogapi.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1006/api/
        /// </summary>
        public static string BlogApiUrl
        {
            get
            {
                if (string.IsNullOrEmpty(BlogWebDomain))
                    return null;

                return BlogWebDomain.Replace("blog.", "blogapi.")
                        .Replace(":1005", ":1006").Replace(":1015", ":1016") + "api/";
            }
        }

        /// <summary>
        /// 获取租户的Api接口地址：http://[tenantName].api.kcloudy.com/api/
        /// </summary>
        /// <param name="tenantName"></param>
        /// <returns></returns>
        public static string GetTenantApiServerUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(ApiServerUrl))
                return null;

            return ApiServerUrl.Replace(TenantConstant.SubDomain, tenantName);
        }

        /// <summary>
        /// SSO登录接口：http://ssoapi.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1002/api/
        /// </summary>
        /// <param name="tenantName"></param>
        /// <returns></returns>
        public static string GetSsoApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(SSOWebDomain))
                return null;

            return SSOWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("sso.", "ssoapi.").Replace(":1001", ":1002").Replace(":1011", ":1012").Replace(":1111", ":1112") + "api/";
        }

        /// <summary>
        /// 获取配置信息接口地址：http://[tenantName].cfgapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:1102/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetConfigApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(CfgWebDomain))
                return null;

            return CfgWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("cfg.", "cfgapi.").Replace(":1101", ":1102").Replace(":1111", ":1112") + "api/";
        }
        /// <summary>
        /// 获取字典信息接口地址：http://[tenantName].dicapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:1104/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetDictionaryApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(DicWebDomain))
                return null;

            return DicWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("dic.", "dicapi.").Replace(":1103", ":1104").Replace(":1113", ":1114") + "api/";
        }
        /// <summary>
        /// 获取应用信息接口地址：http://[tenantName].appapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:1106/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetApplicationApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(AppWebDomain))
                return null;

            return AppWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("app.", "appapi.").Replace(":1105", ":1106").Replace(":1115", ":1116") + "api/";
        }
        /// <summary>
        /// 获取文档接口地址：http://[tenantName].msgapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:2000/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetMessageApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(MsgWebDomain))
                return null;

            return MsgWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("msg.", "msgapi.").Replace(":1107", ":1108").Replace(":1117", ":1118") + "api/";
        }

        /// <summary>
        /// 获取账户信息接口地址：http://[tenantName].accapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:2002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetAccountApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(AccWebDomain))
                return null;

            return AccWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("acc.", "accapi.").Replace(":2001", ":2002").Replace(":2011", ":2012") + "api/";
        }

        /// <summary>
        /// 获取电子合同接口地址：http://[tenantName].econapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:2004/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetEContractApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(EconWebDomain))
                return null;

            return EconWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("econ.", "econapi.").Replace(":2003", ":2004").Replace(":2013", ":2014") + "api/";
        }
        /// <summary>
        /// 获取文档接口地址：http://[tenantName].docapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:2006/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetDocumentApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(DocWebDomain))
                return null;

            return DocWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("doc.", "docapi.").Replace(":2005", ":2006").Replace(":2015", ":2016") + "api/";
        }
        /// <summary>
        /// 获取文档接口地址：http://[tenantName].hrapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:2008/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetHumanResourceApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(HrWebDomain))
                return null;

            return HrWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("hr.", "hrapi.").Replace(":2007", ":2008").Replace(":2017", ":2018") + "api/";
        }

        /// <summary>
        /// 获取客户信息接口地址：http://[tenantName].crmapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:3002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetCrmApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(CrmWebDomain))
                return null;

            return CrmWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("crm.", "crmapi.").Replace(":3001", ":3002").Replace(":3011", ":3012") + "api/";
        }
        /// <summary>
        /// 获取供应商信息接口地址：http://[tenantName].srmapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:3004/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetSrmApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(SrmWebDomain))
                return null;

            return SrmWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("srm.", "srmapi.").Replace(":3003", ":3004").Replace(":3013", ":3014") + "api/";
        }
        /// <summary>
        /// 获取产品信息接口地址：http://[tenantName].prdapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:3006/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetPrdApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(PrdWebDomain))
                return null;

            return PrdWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("prd.", "prdapi.").Replace(":3005", ":3006").Replace(":3015", ":3016") + "api/";
        }
        /// <summary>
        /// 获取物料信息接口地址：http://[tenantName].Pmcapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:3008/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetPmcApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(PmcWebDomain))
                return null;

            return PmcWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("pmc.", "pmcapi.").Replace(":3007", ":3008").Replace(":3017", ":3018") + "api/";
        }

        /// <summary>
        /// 获取门户站点接口地址：http://[tenantName].portalapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:4002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetPortalApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(PortalWebDomain))
                return null;

            return PortalWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("portal.", "portalapi.").Replace(":4001", ":4002") + "api/";
        }
        /// <summary>
        /// 获取采购订单信息接口地址：http://[tenantName].somapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:4004/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetSomApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(SomWebDomain))
                return null;

            return SomWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("som.", "somapi.").Replace(":4003", ":4004").Replace(":4013", ":4014") + "api/";
        }

        /// <summary>
        /// 获取销售订单信息接口地址：http://[tenantName].pomapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:4006/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetPomApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(PomWebDomain))
                return null;

            return PomWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("pom.", "pomapi.").Replace(":4005", ":4006").Replace(":4015", ":4016") + "api/";
        }

        /// <summary>
        /// 获取物料信息接口地址：http://[tenantName].wmsapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:4008/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetWmsApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(WmsWebDomain))
                return null;

            return WmsWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("wms.", "wmsapi.").Replace(":4007", ":4008").Replace(":4017", ":4018") + "api/";
        }

        /// <summary>
        /// 获取登录用户接口地址：http://[tenantName].jrapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:5002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetMarketApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(JRWebDomain))
                return null;

            return JRWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("jr.", "jrapi.").Replace(":5001", ":5002") + "api/";
        }
        /// <summary>
        /// 获取登录培训管理接口地址：http://[tenantName].trnapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:6002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetTrainApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(TrainWebDomain))
                return null;

            return TrainWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("trn.", "trnapi.").Replace(":6001", ":6002") + "api/";
        }
        /// <summary>
        /// 获取登录用户接口地址：http://[tenantName].flowapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:7002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetFlowApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(FlowWebDomain))
                return null;

            return FlowWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("flow.", "flowapi.").Replace(":7001", ":7002") + "api/";
        }
        /// <summary>
        /// 获取登录用户接口地址：http://[tenantName].payapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:8002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetPaymentApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(PayWebDomain))
                return null;

            return PayWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("pay.", "payapi.").Replace(":8001", ":8002") + "api/";
        }
        /// <summary>
        /// 获取登录用户接口地址：http://[tenantName].wxapi.kcloudy.com/api/
        ///     本地测试接口地址：http://[tenantName].localhost:9002/api/
        /// </summary>
        /// <param name="tenantName">租户代码</param>
        /// <returns></returns>
        public static string GetWeixinApiUrl(string tenantName)
        {
            if (string.IsNullOrEmpty(WXWebDomain))
                return null;

            return WXWebDomain.Replace(TenantConstant.SubDomain, tenantName)
                .Replace("wx.", "wxapi.").Replace(":9001", ":9002") + "api/";
        }
        #endregion

        #region SSO

        private static StoreName? _certStoreName;
        public static StoreName CertStoreName
        {
            get
            {
                if (_certStoreName == null)
                    _certStoreName = (StoreName)Enum.Parse(typeof(StoreName), "My");
                return (StoreName)_certStoreName;
            }
            set { _certStoreName = value; }
        }

        private static StoreLocation? _certLocation;
        public static StoreLocation CertLocation
        {
            get
            {
                if (_certLocation == null)
                    _certLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), "LocalMachine");
                return (StoreLocation)_certLocation;
            }
            set { _certLocation = value; }
        }

        public static string CertThumbprint { get; set; }

        #endregion

        #region Upload config


        /// <summary>
        /// 上传配置
        /// </summary>
        public static UploadConfig UploadConfig { get; set; }

        #endregion

        /// <summary>
        /// 系统环境类型：Dev、Test、Beta、Production
        /// </summary>
        public static SystemType SystemType { get; set; }

        public static bool IsDevelopment
        {
            get
            {
                return SystemType == SystemType.Dev;
            }
        }

        public static void InitGlobalConfig(IConfiguration configuration)
        {
            #region Basic Settings
            var config = configuration.GetSection("AppSettings");
            ApplicationId = config["ApplicationId"];
            if (!String.IsNullOrEmpty(ApplicationId))
            {
                Guid appid;
                var issccess = Guid.TryParse(ApplicationId, out appid);
                if (issccess)
                    ApplicationGuid = appid;
            }
            ApplicationName = config["ApplicationName"];
            ApplicationWebSiteName = config["ApplicationWebSiteName"];

            EncryptKey = config["EncryptKey"];
            BlobStorage = config["BlobStorage"];
            AdminEmails = config["AdminEmails"];
            TempFilePath = config["TempFilePath"];

            DatabaseConnectionString = config["DatabaseConnectionString"];
            MySqlConnectionString = config["MySqlConnectionString"];
            StorageConnectionString = config["StorageConnectionString"];
            QueueConnectionString = config["QueueConnectionString"];
            NoSqlConnectionString = config["NoSqlConnectionString"];
            RedisConnectionString = config["RedisConnectionString"];
            ServiceBusConnectionString = config["ServiceBusConnectionString"];

            SSOWebDomain = config["SSOWebDomain"].EndWithSlash();
            ResWebDomain = config["ResWebDomain"].EndWithSlash();

            ClientId = config["ClientId"];
            ClientSecret = config["ClientSecret"];

            WeixinAppKey = config["WeixinAppKey"];
            WeixinAppSecret = config["WeixinAppSecret"];
            WeixinAppToken = config["WeixinAppToken"];

            ZZZLicenseName = config["ZZZLicenseName"];
            ZZZLicenseKey = config["ZZZLicenseKey"];

            CertThumbprint = config["ZZZLicenseKey"];
            #endregion

            #region SystemType
            var webDomain = SSOWebDomain?.ToLower();
            if (webDomain.Contains("localhost")
                || webDomain.Contains("localsso.")
                || webDomain.Contains("devsso."))
            {
                SystemType = SystemType.Dev;
            }
            else if (webDomain.Contains("testsso."))
            {
                SystemType = SystemType.Test;
            }
            else if (webDomain.Contains("betasso."))
            {
                SystemType = SystemType.Beta;
            }
            else
            {
                SystemType = SystemType.Product;
            }
            #endregion

            InitGlobalConfigWithApiData(configuration, null);
        }

        public static void InitGlobalConfigWithApiData(IConfiguration configuration, GlobalConfigData data)
        {
            #region Basic Settings
            var config = configuration.GetSection("AppSettings");

            //EncryptKey = config["EncryptKey"] ?? (data?.EncryptKey);
            BlobStorage = config["BlobStorage"] ?? (data?.BlobStorage);
            AdminEmails = config["AdminEmails"] ?? (data?.AdminEmails);
            TempFilePath = config["TempFilePath"] ?? (data?.TempFilePath);

            #endregion

            #region Domain

            SSOWebDomain = (config["SSOWebDomain"] ?? (data?.SSOWebDomain)).EndWithSlash();
            AdminWebDomain = (config["AdminWebDomain"] ?? (data?.AdminWebDomain)).EndWithSlash();
            BlogWebDomain = (config["BlogWebDomain"] ?? (data?.BlogWebDomain)).EndWithSlash();

            AppWebDomain = (config["AppWebDomain"] ?? (data?.AppWebDomain)).EndWithSlash();
            CfgWebDomain = (config["CfgWebDomain"] ?? (data?.CfgWebDomain)).EndWithSlash();
            DicWebDomain = (config["DicWebDomain"] ?? (data?.DicWebDomain)).EndWithSlash();
            MsgWebDomain = (config["MsgWebDomain"] ?? (data?.MsgWebDomain)).EndWithSlash();

            AccWebDomain = (config["AccWebDomain"] ?? (data?.AccWebDomain)).EndWithSlash();
            EconWebDomain = (config["EconWebDomain"] ?? (data?.EconWebDomain)).EndWithSlash();
            DocWebDomain = (config["DocWebDomain"] ?? (data?.DocWebDomain)).EndWithSlash();
            HrWebDomain = (config["HrWebDomain"] ?? (data?.HrWebDomain)).EndWithSlash();

            CrmWebDomain = (config["CrmWebDomain"] ?? (data?.CrmWebDomain)).EndWithSlash();
            SrmWebDomain = (config["SrmWebDomain"] ?? (data?.SrmWebDomain)).EndWithSlash();
            PrdWebDomain = (config["PrdWebDomain"] ?? (data?.PrdWebDomain)).EndWithSlash();
            PmcWebDomain = (config["PmcWebDomain"] ?? (data?.PmcWebDomain)).EndWithSlash();

            PortalWebDomain = (config["PortalWebDomain"] ?? (data?.PortalWebDomain)).EndWithSlash();
            SomWebDomain = (config["SomWebDomain"] ?? (data?.SomWebDomain)).EndWithSlash();
            PomWebDomain = (config["PomWebDomain"] ?? (data?.PomWebDomain)).EndWithSlash();
            WmsWebDomain = (config["WmsWebDomain"] ?? (data?.WmsWebDomain)).EndWithSlash();

            JRWebDomain = (config["JRWebDomain"] ?? (data?.JRWebDomain)).EndWithSlash();
            TrainWebDomain = (config["TrainWebDomain"] ?? (data?.TrainWebDomain)).EndWithSlash();

            FlowWebDomain = (config["FlowWebDomain"] ?? (data?.FlowWebDomain)).EndWithSlash();
            PayWebDomain = (config["PayWebDomain"] ?? (data?.PayWebDomain)).EndWithSlash();

            WXWebDomain = (config["WXWebDomain"] ?? (data?.WXWebDomain)).EndWithSlash();
            ResWebDomain = (config["ResWebDomain"] ?? (data?.ResWebDomain)).EndWithSlash();
            ApiWebDomain = (config["ApiWebDomain"] ?? (data?.ApiWebDomain)).EndWithSlash();

            Applications = ApplicationConstant.GetAllApplications();

            CurrentApplication = Applications.FirstOrDefault(m => m.AppId == ApplicationGuid);
            #endregion

            #region UploadConfig
            var uploadConfig = configuration.GetSection("UploadConfig");
            var imageMaxSizeString = uploadConfig["ImageMaxSize"];
            var imageMaxSize = 10;
            if (!string.IsNullOrEmpty(imageMaxSizeString))
            {
                int size;
                var issccess = int.TryParse(imageMaxSizeString, out size);
                if (issccess)
                    imageMaxSize = size;
            }
            else if(data != null && data.UploadConfig != null)
            {
                imageMaxSize = data.UploadConfig.ImageMaxSize;
            }

            var fileMaxSizeString = uploadConfig["FileMaxSize"];
            var fileMaxSize = 10;
            if (!string.IsNullOrEmpty(fileMaxSizeString))
            {
                int size;
                var issccess = int.TryParse(fileMaxSizeString, out size);
                if (issccess)
                    fileMaxSize = size;
            }
            else if(data != null && data.UploadConfig != null)
            {
                fileMaxSize = data.UploadConfig.FileMaxSize;
            }

            UploadConfig = new UploadConfig()
            {
                ImageMaxSize = imageMaxSize,
                ImageExt = uploadConfig["ImageExt"] ?? data?.UploadConfig?.ImageExt,
                FileMaxSize = fileMaxSize,
                FileExt = uploadConfig["FileExt"] ?? data?.UploadConfig?.FileExt
            };

            #endregion
        }
    }

    public enum SystemType
    {
        Dev = 0,
        Test = 1,
        Beta = 2,
        Product = 3,
    }

    public class UploadConfig
    {
        public int ImageMaxSize { get; set; }

        public int FileMaxSize { get; set; }

        public string ImageExt { get; set; }

        public string FileExt { get; set; }
    }

    public class ApplicationInfo
    {
        public ApplicationInfo(Guid id, string name, string domain, string scope)
        {
            AppId = id;
            AppName = name;
            AppDomain = domain;
            AppScope = scope;
        }
        public Guid AppId { get; set; }

        public string AppName { get; set; }

        public string AppDomain { get; set; }

        public string AppScope { get; set; }
    }

    public class GlobalConfigData
    {
        #region AppSetting configuration
        //public string EncryptKey { get; set; }
        public  string BlobStorage { get; set; }
        public  string AdminEmails { get; set; }
        public  string TempFilePath { get; set; }

        #endregion

        #region Domain
        public  List<ApplicationInfo> Applications { get; set; }

        /// <summary>
        /// subdomain的sso地址：http://sso.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1001/
        /// </summary>
        public  string SSOWebDomain { get; set; }
        /// <summary>
        /// subdomain的admin地址：http://admin.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1003
        /// </summary>
        public  string AdminWebDomain { get; set; }
        /// <summary>
        /// subdomain的blog地址：http://blog.kcloudy.com/
        ///     本地测试接口地址：http://localhost:1005
        /// </summary>
        public string BlogWebDomain { get; set; }

        /// <summary>
        /// subdomain的config地址：http://subdomain.cfg.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1101/
        /// </summary>
        public string CfgWebDomain { get; set; }
        /// <summary>
        /// subdomain的dictionary地址：http://subdomain.dic.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1103/
        /// </summary>
        public  string DicWebDomain { get; set; }
        /// <summary>
        /// subdomain的app地址：http://subdomain.app.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1105
        /// </summary>
        public  string AppWebDomain { get; set; }
        
        /// <summary>
        /// subdomain的dictionary地址：http://subdomain.msg.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:1109/
        /// </summary>
        public string MsgWebDomain { get; set; }

        /// <summary>
        /// subdomain的account地址：http://subdomain.acc.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2001/
        /// </summary>
        public  string AccWebDomain { get; set; }

        /// <summary>
        /// subdomain的account地址：http://subdomain.econ.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2003/
        /// </summary>
        public string EconWebDomain { get; set; }
        /// <summary>
        /// subdomain的document地址：http://subdomain.doc.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2005/
        /// </summary>
        public string DocWebDomain { get; set; }
        /// <summary>
        /// subdomain的Hr地址：http://subdomain.hr.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:2007/
        /// </summary>
        public string HrWebDomain { get; set; }

        /// <summary>
        /// subdomain的crm地址：http://subdomain.crm.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3001/
        /// </summary>
        public string CrmWebDomain { get; set; }

        /// <summary>
        /// subdomain的crm地址：http://subdomain.srm.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3003/
        /// </summary>
        public string SrmWebDomain { get; set; }

        /// <summary>
        /// subdomain的prd地址：http://subdomain.prd.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3005/
        /// </summary>
        public string PrdWebDomain { get; set; }

        /// <summary>
        /// subdomain的pmc地址：http://subdomain.pmc.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:3007/
        /// </summary>
        public string PmcWebDomain { get; set; }

        /// <summary>
        /// subdomain的电商地址：http://subdomain.portal.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4001/
        /// </summary>
        public  string PortalWebDomain { get; set; }
        /// <summary>
        /// subdomain的som地址：http://subdomain.som.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4003/
        /// </summary>
        public string SomWebDomain { get; set; }

        /// <summary>
        /// subdomain的pom地址：http://subdomain.pom.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4005/
        /// </summary>
        public string PomWebDomain { get; set; }

        /// <summary>
        /// subdomain的wms地址：http://subdomain.wms.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:4007/
        /// </summary>
        public string WmsWebDomain { get; set; }

        /// <summary>
        /// subdomain的融资地址：http://subdomain.market.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:5001/
        /// </summary>
        public string JRWebDomain { get; set; }

        /// <summary>
        /// subdomain的erp地址：http://subdomain.erp.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:6001/
        /// </summary>
        public  string TrainWebDomain { get; set; }

        /// <summary>
        /// subdomain的工作流地址：http://subdomain.flow.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:7001/
        /// </summary>
        public  string FlowWebDomain { get; set; }
        /// <summary>
        /// subdomain的sso地址：http://subdomain.pay.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:8001/
        /// </summary>
        public  string PayWebDomain { get; set; }
        
        /// <summary>
        /// subdomain的微信地址：http://subdomain.wx.kcloudy.com/
        ///     本地测试接口地址：http://subdomain.localhost:9001/
        /// </summary>
        public  string WXWebDomain { get; set; }

        /// <summary>
        /// subdomain的资源地址：http://resource.kcloudy.com/
        ///     本地测试接口地址：http://localhost:9009/
        /// </summary>
        public string ResWebDomain { get; set; }
        #endregion

        #region Api Url

        /// <summary>
        /// subdomain的接口地址，无api/后缀：http://subdomain.api.kcloudy.com/
        /// </summary>
        public  string ApiWebDomain { get; set; }

        /// <summary>
        /// subdomain的接口地址：http://subdomain.api.kcloudy.com/api/
        /// </summary>
        public  string ApiServerUrl { get; set; }
        
        #endregion

        #region Upload config


        /// <summary>
        /// 上传配置
        /// </summary>
        public  UploadConfig UploadConfig { get; set; }

        #endregion
    }
}
